package org.languageguide.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.languageguide.storage.DBConnector;
import org.languageguide.versioncontrol.Editable;


public class User{

	private String name;
	private String email;
	private int id;
	private String language;
	private String editingLanguage;
	private String password;
	//private String hashedPw;
	public final static String sessionId = "User";
	//private HttpSession session = null;
	private Editable editable;
	
	public User(ResultSet rs) throws SQLException{
		name = rs.getString("fullname");
		email = rs.getString("email");
		password = rs.getString("password");
		id = rs.getInt("userid");
		System.out.println("email" + email);
		editingLanguage = rs.getString("editing_language");
		language = rs.getString("language");
		System.out.println("language" + language);
		
		//this.session = session;
		if (language == null){
			language ="en";
		}
	}
	
	public User(String name, String email, String password, String lang){
		this.name = name;
		this.email = email;
		this.password = password;
		this.editingLanguage = lang;
	}
	
    public String getHashedPw(){
    	return PasswordUtility.getHash(password);
    }
	
	public boolean isActive(){
		//if (session == null){
			//return
		//}
		return true;
	}

	public void store(){
		String sql = "INSERT INTO Users2(email, password, code, hashed_password, editing_language, fullname) VALUES (?, ?, ?, ?, ?)";
		DBConnector connector = DBConnector.getInstance();
		Connection connection = connector.getConnection();
		String hash = PasswordUtility.getHash(password);
		try {
			PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			statement.setString(1, email);
			statement.setString(2, password);
			String code = PasswordUtility.getHash(email);
			statement.setString(3, code);
			statement.setString(4, hash);
			statement.setString(5, editingLanguage);
			statement.setString(6, name);
			statement.executeUpdate();
			ResultSet keys = statement.getGeneratedKeys();
			keys.next();
			id = keys.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		finally{
			connector.close(connection);
		}
	}

	public String getFullName(){
		return name; 
	}
	
	public String getEmail(){
		return email; 
	}

	public String getEditingLanguage(){
		return editingLanguage; 
	}
	
	public void setEditingLanguage(String language){
		editingLanguage = language; 
	}	

	public String getLanguage(){
		return language;
	}

	public int getId(){
		return id;
	}
	
	public Integer getIdInteger(){
		return new Integer(id);
	}
	

	public void setLock(Editable editable){
		this.editable = editable;
	}
	public boolean hasLock(){
		return (editable != null);
	}
	
	public boolean hasOtherLock(Editable categoryToCheck){
		if(editable == null)
			return false;
		if (editable.getKey().equals(categoryToCheck.getKey()))
			return false;
		return true;
	}
	
	public void unlockCategories(){
		if (editable != null){
			System.out.println("category unlocked" + editable.getKey());
			editable.finishEdit();
			editable = null;
		}
	}
}